﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows;
using System.Windows.Forms;

namespace RemotePC.NET
{
    class SettingXML
    {
        private string name;
        public string Name
        {
            get { return name; }
        }

        private string path;
        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        private XmlDocument doc;

        public SettingXML(string strPath, string strName)
        {
            path = strPath;
            name = strName;            
        }

        public void InitXML()
        {
            doc = new XmlDocument();
            doc.Load(path + name);
        }

        public void BacktoDefault()
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.NewLineOnAttributes = true;
            XmlWriter writer = XmlWriter.Create(path + name, settings);
            writer.WriteStartDocument();
            // 开始写入元素及属性。
            writer.WriteStartElement("setting");
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-01");
            writer.WriteAttributeString("path", "192.168.1.19");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-02");
            writer.WriteAttributeString("path", "192.168.1.2");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-03");
            writer.WriteAttributeString("path", "192.168.1.3");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-04");
            writer.WriteAttributeString("path", "192.168.1.4");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-05");
            writer.WriteAttributeString("path", "192.168.1.5");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-06");
            writer.WriteAttributeString("path", "192.168.1.6");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-07");
            writer.WriteAttributeString("path", "192.168.1.7");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-08");
            writer.WriteAttributeString("path", "192.168.1.8");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-09");
            writer.WriteAttributeString("path", "192.168.1.9");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-10");
            writer.WriteAttributeString("path", "192.168.1.10");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteStartElement("computer");
            writer.WriteAttributeString("name", "PC-11");
            writer.WriteAttributeString("path", "192.168.1.11");
            writer.WriteAttributeString("username", "administrator");
            writer.WriteAttributeString("password", "hust");
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteEndDocument();
            // clean up.
            writer.Flush();
            writer.Close();
        }

        public string ReadSetting(string strName, string strAttribute)
        {
            string result = null;

            XmlNodeList nodeLst = doc.GetElementsByTagName("computer");
            if (nodeLst != null)
            {
                foreach (XmlNode node in nodeLst)
                {
                    XmlAttributeCollection xac = node.Attributes;
                    if (xac["name"].Value == strName)
                    {
                        result = xac[strAttribute].Value;
                    }
                }
            }  
            return result;
        }

        public string WriteSetting(string strName, string strAttribute, string strValue)
        {
            string result = null;

            XmlNodeList nodeLst = doc.GetElementsByTagName("computer");
            if (nodeLst != null)
            {
                foreach (XmlNode node in nodeLst)
                {
                    XmlAttributeCollection xac = node.Attributes;
                    if (xac["name"].Value == strName)
                    {
                        result = xac[strAttribute].Value;
                        xac[strAttribute].Value = strValue;
                        MessageBox.Show(result);
                    }
                }
            }
            return result;
        }
    }
}
